//
//  YYMusicArticalViewController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
//  乐文总控制器

import UIKit
import WebKit
class YYMusicArticalViewController: UIViewController,UIScrollViewDelegate {

    static var scrollViewHeight = SCREEN_HEIGHT - 144 - 64 - 40;
   static var webView:WKWebView?;
    
    var segmentButton:STSegementButton?
    
    var scrollView:UIScrollView! = UIScrollView.init();
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white;
        
        YYMusicArticalViewController.webView = WKWebView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 64));
        YYMusicArticalViewController.webView?.alpha = 0.7;
        DispatchQueue.global().async {
            let request:URLRequest = URLRequest.init(url: URL.init(string: "http://wawa.fm/webview/yuyin_content.html?id=995957849")!);
           _ =  YYMusicArticalViewController.webView?.load(request);
 
        }
        YYMusicArticalViewController.webView?.isUserInteractionEnabled = false;
        self.view.addSubview(YYMusicArticalViewController.webView!);
        
        let titleLable = STLabel.init(frame: .init(x: 0, y: 0, width: 40, height: 18), text: "乐文", textColor: .white, font: 18, isSizetoFit: false, textAlignment: .center);
        titleLable?.backgroundColor = .clear;
        self.navigationItem.titleView = titleLable;
        
        //self.setRightItemWithTitle("播放按钮", titleColor: .white);
        
        self.automaticallyAdjustsScrollViewInsets = false;
        //segemant
        let secmentbackView = UIView.init(frame: .init(x: 0, y: 100, width: SCREEN_WIDTH, height: 40));
        secmentbackView.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame:secmentbackView.bounds ).withAlphaComponent(0.6);
        self.view.addSubview(secmentbackView);
        self.segmentButton = STSegementButton.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH - 30, height: 40), andTitle: ["乐文","猎乐","遇见","音乐人"], handle: { (but) -> Void in
            
            self.scrollView.setContentOffset(CGPoint.init(x: SCREEN_WIDTH * CGFloat((but?.tag)!), y: 0), animated: true);
        });
        self.segmentButton?.butTitleColor = SecendTextColor;
        self.segmentButton?.lineColor = .white;
        self.segmentButton?.backgroundColor = .clear;
        self.segmentButton?.butTitleSelectedColor = .white;
        self.segmentButton?.lineSelectedColor = .white;
        self.segmentButton?.autoMoveWithClic = false;
        self.segmentButton?.isShowSpringAnimation = false;
        self.segmentButton?.centerX = SCREEN_WIDTH / 2;
        secmentbackView.addSubview(self.segmentButton!);
        
        //scrollview
        
        self.scrollView = UIScrollView.init(frame: .init(x: 0, y: 140, width: SCREEN_WIDTH, height: YYMusicArticalViewController.scrollViewHeight));
        self.scrollView.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.scrollView.bounds);
        self.scrollView.contentSize = .init(width: SCREEN_WIDTH * 4, height: 0);
        self.scrollView.delegate = self;
        self.scrollView.isPagingEnabled = true;
        self.scrollView.isScrollEnabled = true;
        self.scrollView.backgroundColor = .clear;
        STLayoutViewController.defaultLayout().panGestureRecognizer.require(toFail: self.scrollView.panGestureRecognizer);
        self.view.addSubview(self.scrollView);
        //child
        let articleVC:YYMusicChildArticleController = YYMusicChildArticleController.init();
        self.scrollView.addSubview(articleVC.view);
        self.addChildViewController(articleVC);
        
        let findMusicVC:YYMusicChildFindController = YYMusicChildFindController.init();
        findMusicVC.view.left = SCREEN_WIDTH;
        self.scrollView.addSubview(findMusicVC.view);
        self.addChildViewController(findMusicVC);
        
        
        let meetVC:YYMusicChildMeetController = YYMusicChildMeetController.init();
        meetVC.view.left = SCREEN_WIDTH * 2;
        self.scrollView.addSubview(meetVC.view);
        self.addChildViewController(meetVC);
        
        let musicerVC:YYMusicChildMusicerController = YYMusicChildMusicerController.init();
        musicerVC.view.left = SCREEN_WIDTH * 3;
        self.scrollView.addSubview(musicerVC.view);
        self.addChildViewController(musicerVC);

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        YYDefaultMusicAnimationView.sharedInstance.isHidden = false;
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        YYDefaultMusicAnimationView.sharedInstance.isHidden = false;
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated);
    }

    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.scrollView {
            let butwidth = (SCREEN_WIDTH - 30) / 4 ;
            self.segmentButton?.lineSelectedView.centerX =  butwidth/2 + self.scrollView.contentOffset.x/SCREEN_WIDTH * butwidth;
        }
        
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.scrollView {
            let num = self.scrollView.contentOffset.x / SCREEN_WIDTH;
            self.segmentButton?.cureentIndex = Int(num);
        }
        
    }
    // MARK:Action Method
    override func rightBarAction(_ sender: Any!) {
        STLayoutViewController.defaultLayout().showRightViewController(true);
    }



}
